package com.firefightingiot.firefightingiot.web.web;

import cn.hutool.json.JSONObject;
import com.firefightingiot.firefightingiot.common.base.BaseController;
import com.firefightingiot.firefightingiot.common.utils.appkit.ConfigKit;
import com.firefightingiot.firefightingiot.support.annotaion.Action;
import com.firefightingiot.firefightingiot.support.annotaion.At;
import com.firefightingiot.firefightingiot.support.annotaion.Import;
import com.firefightingiot.firefightingiot.support.model.RespEntity;
import com.firefightingiot.firefightingiot.web.service.UploadFileService;
import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Map;

/**
 * 消防物联网报警控制器
 */
@Action
@RequestMapping(value="uploadFile")
public class UploadFileController extends BaseController {
    @Resource
    public UploadFileService uploadFileService;

    /**
     * @return
     * @Title: getTemplte <br>
     * @Description: (下载模板) <br>
     * @author 石延杰<br>
     */
    @At(value = "getTemplte")
    public ResponseEntity<byte[]> getTemplate(@RequestParam Map<String, Object> params) throws Exception {
        return uploadFileService.getTemplate(params);
    }

    /**
     * @return
     * @Title: upload <br>
     * @Description: (导入) <br>
     * @author 石延杰<br>
     */
    @Import(value = "file", startLine = 2, titleLine = 1)
    @At(value = "deviceImports", method = RequestMethod.POST)
    public RespEntity upload(@RequestParam Map<String, Object> params) throws Exception {
        return uploadFileService.addUpload(params);
    }

    /**
     * @Author YangYu
     * @Description 设备导入
     * @Date Created in 15:48 2023/4/14
     * @Param [params]
     */

    @Import(value = "file", startLine = 2, titleLine = 1)
    @At(value = "deviceImport", method = RequestMethod.POST, param = { "businessId","deviceClassify","deviceType","productId"})
    public RespEntity deviceImport(@RequestParam Map<String, Object> params) throws Exception {
        return uploadFileService.deviceImport(params);
    }
    /**
     *
     * @param fileName
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/downloadExcel")
    public ResponseEntity<byte[]> downloadExcel(@RequestParam("fileName") String fileName) throws IOException {
        String fileSavePath = ConfigKit.getStr("excelSavePath", "excel");
        String path = fileSavePath + fileName;
        File file = new File(path);
        HttpHeaders headers = new HttpHeaders();
        //下载显示的文件名，解决中文名称乱码问题
        String downloadFielName = new String(fileName.getBytes("UTF-8"), "iso-8859-1");
        //通知浏览器以attachment（下载方式）打开图片
        headers.setContentDispositionFormData("attachment", downloadFielName);
        //application/octet-stream ： 二进制流数据（最常见的文件下载）。
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),
                headers, HttpStatus.CREATED);
    }

    /**
     * 设备导出
     * @param params
     * @return
     * @throws Exception
     */
    @At(value = "getDeviceTemplte")
    public ResponseEntity<byte[]> getDeviceTemplte(@RequestParam Map<String, Object> params) throws Exception {
        return uploadFileService.getDeviceTemplte(params);
    }

    /**
     * 设备导出
     * @param params
     * @return
     * @throws Exception
     */
    @At(value = "getDeviceExport")
    public RespEntity getDeviceExport(@RequestParam Map<String, Object> params) throws Exception {
        return uploadFileService.getDeviceExport(params);
    }

    /**
     * 报警记录导出
     * @param params
     * @return
     * @throws Exception
     */
    @At(value = "exportDeviceAlarmLog")
    public ResponseEntity<byte[]> exportDeviceAlarmLog(@RequestParam Map<String, Object> params) throws Exception {
        return uploadFileService.exportDeviceAlarmLog(params);
    }

    /**
     * @Author YangYu
     * @Description led视频上传
     * @Date Created in 16:10 2021/9/28
     * @Param [jsonParam, request]
     */

    @At(value = "uploadLedVideoFile", method = RequestMethod.POST)
    public RespEntity uploadLedVideoFile(String number,String homeId,  HttpServletRequest request) {
        return  uploadFileService.uploadLedVideoFile(number,homeId,request);
    }

    /**
     * @Author YangYu
     * @Description ip音频上传
     * @Date Created in 15:57 2021/9/28
     * @Param [request]
     */

    @At(value = "uploadIpAudioFile", method = RequestMethod.POST)
    public RespEntity uploadIpAudioFile(String homeId, HttpServletRequest request) {
        return  uploadFileService.uploadIpAudioFile(homeId, request);
    }

    /**
     * @Author YangYu
     * @Description 卸油证据链上传
     * @Date Created in 13:53 2021/11/19
     * @Param [request]
     */

    @At(value = "uploadChainOfEvidenceFile", method = RequestMethod.POST)
    public RespEntity uploadChainOfEvidenceFile(HttpServletRequest request) {
        return  uploadFileService.uploadChainOfEvidenceFile(request);
    }
    /**
     * @Author YangYu
     * @Description 维保设备导入
     * @Date Created in 11:44 2022/9/13
     * @Param [params]
     */

    @Import(value = "file", startLine = 2, titleLine = 1)
    @At(value = "maintenanceDeviceImport", method = RequestMethod.POST)
    public RespEntity maintenanceDeviceImport(@RequestParam Map<String, Object> params) throws Exception {
        return uploadFileService.maintenanceDeviceImport(params);
    }


    /**
     * @Author YangYu
     * @Description 单位导入
     * @Date Created in 10:46 2022/9/13
     * @Param [params]
     */
    @Import(value = "file", startLine = 2, titleLine = 1)
    @At(value = "businessImport", method = RequestMethod.POST)
    public RespEntity businessImport(@RequestParam Map<String, Object> params) throws Exception {
        return uploadFileService.businessImport(params);
    }
    /**
     * @Author YangYu
     * @Description 单位导入模板下载
     * @Date Created in 15:40 2022/10/12
     * @Param [params]
     */

    @At(value = "getBusinessTemplate")
    public ResponseEntity<byte[]> getBusinessTemplate(@RequestParam Map<String, Object> params) throws Exception {
        return uploadFileService.getBusinessTemplate(params);
    }

    @At(value = "exportWaterColumnBoxDataLog", method = RequestMethod.GET)
    public RespEntity exportWaterColumnBoxDataLog(@RequestParam Map<String, Object> params) {
        return uploadFileService.exportWaterColumnBoxDataLog(params);
    }
}
